import {
  IsDefined,
  IsEnum,
  IsNotEmpty,
  IsNumberString,
  IsOptional,
} from 'class-validator';
import { PaginDto } from 'src/commen/dto';
import { DtoValidation, IsDayjsFormat } from 'src/decorators/custom-decorator';
import { BookingStatus } from '../entities/booking.entity';
import { Transform } from 'class-transformer';
import * as dayjs from 'dayjs';
import { PartialType } from '@nestjs/swagger';

@DtoValidation({ type: 'query' })
export class ListQueryDto extends PaginDto {
  @IsOptional({ always: true })
  userId?: number;

  @IsOptional({ always: true })
  roomId: number;

  @IsEnum(BookingStatus, { message: '格式不正确' })
  @IsOptional({ always: true })
  status?: BookingStatus;
}

@DtoValidation({ groups: ['create'] })
export class CreateBookingDto {
  @Transform(({ value }) => dayjs(value, 'YYYY-MM-DD HH:mm:ss').toDate())
  @IsDayjsFormat('YYYY-MM-DD HH:mm:ss', {
    message: `startTime格式错误`,
  })
  @IsNotEmpty({ groups: ['create'] })
  @IsOptional({ groups: ['update'] })
  startTime: Date;

  @Transform(({ value }) => dayjs(value, 'YYYY-MM-DD HH:mm:ss').toDate())
  @IsDayjsFormat('YYYY-MM-DD HH:mm:ss', {
    message: `startTime格式错误`,
  })
  @IsNotEmpty({ groups: ['create'] })
  @IsOptional({ groups: ['update'] })
  endTime: Date;

  @IsEnum(BookingStatus, { message: '格式不正确' })
  @IsOptional({ always: true })
  status?: BookingStatus;

  @IsOptional({ always: true })
  note?: string;

  @IsNumberString()
  @IsNotEmpty({ groups: ['create'] })
  @IsOptional({ groups: ['update'] })
  userId: string;

  @IsNumberString()
  @IsNotEmpty({ groups: ['create'] })
  @IsOptional({ groups: ['update'] })
  roomId: string;
}

@DtoValidation({ groups: ['update'] })
export class UpdateBookingDto extends PartialType(CreateBookingDto) {
  @IsDefined({ message: 'Id必须指定', groups: ['update'] })
  id: number;
}
